package com.stylefeng.guns.gateway.modular.cinema;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.cinema.CinemaServiceAPI;
import com.stylefeng.guns.api.cinema.vo.*;
import com.stylefeng.guns.api.order.OrderServiceAPI;
import com.stylefeng.guns.gateway.modular.cinema.vo.CinemaConditionResponseVO;
import com.stylefeng.guns.gateway.modular.cinema.vo.CinemaFieldResponseVO;
import com.stylefeng.guns.gateway.modular.cinema.vo.CinemaFieldsResponseVO;
import com.stylefeng.guns.gateway.modular.vo.ResponseVO;
import lombok.extern.log4j.Log4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cinema/")
@Log4j
public class CinemaController {
    private static final String IMG_PRE = "http://img.meetingshop.cn/";
    // connections 控制连接数量
    @Reference(cache = "lru",connections = 10)
    private CinemaServiceAPI cinemaServiceAPI;

    @Reference
    private OrderServiceAPI orderServiceAPI;

    /**
     * 查询影院列表-根据条件查询所有影院
     *
     * @param cinemaQueryVO
     * @return
     */
    @RequestMapping("getCinemas")
    public ResponseVO getCinemas(CinemaQueryVO cinemaQueryVO) {
        try {
            Page<CinemaVO> cinemas = cinemaServiceAPI.getCinemas(cinemaQueryVO);
            if (cinemas.getRecords() == null || CollectionUtil.isEmpty(cinemas.getRecords())) {
                return ResponseVO.success("没有影院可查");
            } else {
                return ResponseVO.success(cinemas.getRecords(), "", cinemas.getCurrent(), (int) cinemas.getPages());
            }

        } catch (Exception e) {
            log.error("获取影院列表异常", e);
            return ResponseVO.serviceFail("查询影院列表失败");
        }
    }

    /**
     * 获取影院列表查询条件
     * lru结果缓存
     * @param cinemaQueryVO
     * @return
     */
    @RequestMapping("getCondition")
    public ResponseVO getCondition(CinemaQueryVO cinemaQueryVO) {

        try {
            List<BrandVO> brands = cinemaServiceAPI.getBrands(cinemaQueryVO.getBrandId());
            List<AreaVO> areas = cinemaServiceAPI.getAreas(cinemaQueryVO.getDistrictId());
            List<HallTypeVO> hallTypes = cinemaServiceAPI.getHallTypes(cinemaQueryVO.getHallType());
            CinemaConditionResponseVO cinemaConditionResponseVO = new CinemaConditionResponseVO();
            cinemaConditionResponseVO.setAreaList(areas);
            cinemaConditionResponseVO.setBrandList(brands);
            cinemaConditionResponseVO.setHallTypeList(hallTypes);

            return ResponseVO.success(cinemaConditionResponseVO);
        }catch (Exception e){
            log.error("获取条件列表失败",e);
            return ResponseVO.serviceFail("获取条件列表失败");
        }

    }

    /**
     * 获取播放场次接口
     *
     * @param cinemaId
     * @return
     */
    @RequestMapping("getFields")
    public ResponseVO getFields(Integer cinemaId) {
        try{
            CinemaInfoVO cinemaInfoById = cinemaServiceAPI.getCinemaInfoById(cinemaId);
            List<FilmInfoVO> filmInfoVOS = cinemaServiceAPI.getfilmInfoByCinemaId(cinemaId);
            CinemaFieldsResponseVO cinemaFieldsResponseVO = new CinemaFieldsResponseVO();
            cinemaFieldsResponseVO.setCinemaInfo(cinemaInfoById);
            cinemaFieldsResponseVO.setFilmList(filmInfoVOS);
            return ResponseVO.success(cinemaFieldsResponseVO, IMG_PRE);
        }catch (Exception e){
            log.error("获取播放场次失败",e);
            return ResponseVO.serviceFail("获取播放场次失败");
        }


    }

    /**
     * 获取场次详细信息接口
     *
     * @param cinemaId
     * @param fieldId
     * @return
     */
    @RequestMapping("getFieldInfo")
    public ResponseVO getFieldInfo(Integer cinemaId, Integer fieldId) {
        try{
            CinemaInfoVO cinemaInfoById = cinemaServiceAPI.getCinemaInfoById(cinemaId);
            FilmInfoVO filmInfoByFieldId = cinemaServiceAPI.getFilmInfoByFieldId(fieldId);
            HallInfoVO filmFieldInfo = cinemaServiceAPI.getFilmFieldInfo(fieldId);
            filmFieldInfo.setSoldSeats(orderServiceAPI.getSoldSeatsByFieldId(fieldId));
            CinemaFieldResponseVO cinemaFieldResponseVO = new CinemaFieldResponseVO();
            cinemaFieldResponseVO.setCinemaInfo(cinemaInfoById);
            cinemaFieldResponseVO.setFilmInfo(filmInfoByFieldId);
            cinemaFieldResponseVO.setHallInfo(filmFieldInfo);
            return ResponseVO.success(cinemaFieldResponseVO,IMG_PRE);
        }catch (Exception e){
            log.error("获取选座信息失败",e);
            return ResponseVO.serviceFail("获取选座信息失败");
        }

    }

}
